//
//  tripDetailViewController.m
//  YouGoWeGo
//
//  Created by 范业君 on 14-11-20.
//  Copyright (c) 2014年 LiuFei. All rights reserved.
//

#import "tripDetailViewController.h"
#import "detailTableViewCell.h"
#import "ScheduleController.h"
#import "detaiTriplModel.h"
#import "DWBubbleMenuButton.h"
#import "detailPhotoController.h"

@interface tripDetailViewController ()<UITableViewDelegate, UITableViewDataSource>

@end

@implementation tripDetailViewController

- (void)dealloc
{
 
    [_tableArray release];
    [_tableView release];
    [super dealloc];
}


- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tableArray = [NSMutableArray array];
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.navigationController.navigationBar.translucent = NO;
    [self.view setBackgroundColor:[UIColor colorWithRed:249 / 255.0 green:246 / 255.0  blue:231 / 255.0  alpha:1]];
    [self createTableView];
    [self createButton];
    //[self createSchduleButton];
    [self startAdyn];
//    [self createScrollView];
}

//- (void)createScrollView
//{
//    UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
//    [scroll addSubview:_tableView];
//    [scroll setContentSize:CGSizeMake(0, 1000)];
//    [self.view addSubview:scroll];
//    [scroll release];
//}


#pragma mark - 
#pragma mark 创建tableView
- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 150, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    [_tableView registerClass:[detailTableViewCell class] forCellReuseIdentifier:@"ok"];
    //[_tableView setContentSize:CGSizeMake(1000, 0)];
    [_tableView setRowHeight:327];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView setBackgroundColor:[UIColor colorWithRed:249 / 255.0 green:246 / 255.0  blue:231 / 255.0  alpha:1]];
    [self.view addSubview:_tableView];
    [_tableView release];
}






- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_tableArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    detailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ok"];
    [cell setBackgroundColor:[UIColor colorWithRed:249 / 255.0 green:246 / 255.0  blue:231 / 255.0  alpha:1]];
    detaiTriplModel *model = [_tableArray objectAtIndex:indexPath.row];

    if (indexPath.row == 0 && model.photo_info == nil) {
        cell.userInteractionEnabled = NO;
    }
    
    
    cell.model = model;
    return cell;
   
}


#pragma mark - 
#pragma mark  创建按钮点击进入查看日程
- (void)createSchduleButton
{
    UIButton *abutton = [[UIButton alloc] initWithFrame:CGRectMake(0, 130, self.view.frame.size.width, 20)];
    [abutton setBackgroundColor:[UIColor redColor]];
    [abutton setTitle:@"查看日程" forState:UIControlStateNormal];
    [abutton addTarget:self action:@selector(touchAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:abutton];
    [abutton release];
}

- (void)touchAction:(id)sender
{
    ScheduleController *schedule = [[ScheduleController alloc] init];
    [self.navigationController pushViewController:schedule animated:YES];
    [schedule release];
}



#pragma mark - 
#pragma mark 选中某个cell进入详情
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    detailPhotoController *photo = [[detailPhotoController alloc] init];
    [photo.photoArray addObjectsFromArray:_tableArray];
    photo.num = self.numid;
    [self.navigationController pushViewController:photo animated:YES];
    [photo view];
    [photo.myScroll setContentOffset:CGPointMake(self.view.frame.size.width * indexPath.row, 0)];
    
    [photo release];
}



#pragma mark - 
#pragma mark -  创建下拉按钮
- (void)createButton
{
    DWBubbleMenuButton *button = [[DWBubbleMenuButton alloc] initWithFrame:CGRectMake(0, 0, 50, 50) expansionDirection:DirectionLeft];
    
//    [button setBackgroundColor:[UIColor orangeColor]];
    
    UIButton *aButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];
    [aButton setTitle:@"收藏" forState:UIControlStateNormal];
    [aButton setTag:100];
    [aButton addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    UIButton *bButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];
    [bButton setTitle:@"评论" forState:UIControlStateNormal];
    [bButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [bButton setTag:101];
    
    [bButton addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 20, 50, 50)];
    
    [view setBackgroundColor:[UIColor redColor]];
    [button setHomeButtonView:view];
    
    
    NSMutableArray *arr = [NSMutableArray array];
    [arr addObject:aButton];
    [arr addObject:bButton];
    
    [button addButtons:arr];
//    [self.view addSubview:button];
    
    UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItem = barButton;
    
    [barButton release];
    [aButton release];
    [bButton release];
    [button release];
    [view release];
}

#pragma makr - 点击事件
- (void)clickAction:(id)sender
{
    UIButton *button = (UIButton *)sender;
    switch (button.tag) {
        case 100:
        {
            
        }
            break;
            
        default:
            break;
    }
}

#pragma mark - 
#pragma mark  数据解析
- (void)startAdyn
{
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://api.breadtrip.com/trips/%ld/waypoints/",self.numid]];
    NSLog(@"%@", url);
    
    NSURLRequest *requsest = [NSURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:requsest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        
        NSMutableArray *dayArray = [dic objectForKey:@"days"];
        
        for (NSMutableDictionary *dic1 in dayArray) {
            NSMutableArray *arr = [dic1 objectForKey:@"waypoints"];
            
            for (NSMutableDictionary *dic2 in arr) {
                detaiTriplModel *model = [[detaiTriplModel alloc] init];
                [model setValuesForKeysWithDictionary:dic2];
                
                [_tableArray addObject:model];
                [_tableView reloadData];
                [model release];
            }
           
        }
    }];

}



#pragma mark - 
#pragma mark cell 自适应高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    detaiTriplModel *model = [_tableArray objectAtIndex:indexPath.row ];
    NSMutableDictionary *dic = model.photo_info;
    
    if (dic == nil) {
        return 0;
    }
    UILabel *label = [[UILabel alloc] init];
    CGRect rect = [model.text boundingRectWithSize:CGSizeMake(375, 300) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    [label setFrame:CGRectMake(0, 0, self.view.frame.size.width, rect.size.height)];
    
    NSNumber *h = [dic objectForKey:@"h"];
    NSNumber *w = [dic objectForKey:@"w"];
    CGFloat h1 = [h floatValue];
    CGFloat w1 = [w floatValue];

    CGFloat height = self.view.frame.size.width * h1 / w1;
    return height + label.frame.size.height + 40;
}


















@end
